package com.leonzx.base.controller.fileonline;

import com.leonzx.base.config.pager.Page;
import com.leonzx.base.config.pager.PageInfo;
import com.leonzx.base.controller.base.BaseController;
import com.leonzx.base.entity.system.FileOnline;
import com.leonzx.base.service.system.FileOnlineService;
import com.leonzx.base.service.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @Author:李晓雷
 * @Description:
 * @Date: 18/07/11 14:08
 */
@RestController
@RequestMapping("/ajax/fileonline")
public class AjaxFileOnlineController extends BaseController{
    @Autowired
    private FileOnlineService fileOnlineService;
    @Autowired
    private UserService userService;

    /**
     * 在线文件
     *
     * @param fileOnline
     * @param page
     * @return
     */
    @RequestMapping("/list")
    public Map<String, Object> list(FileOnline fileOnline, @PageInfo(pageSize = Page.PAGE_SIZE, orderBy = "create_time") Page<FileOnline> page) {
        Map<String, Object> result = getMap();
        try {
            List<FileOnline> baseFileOnlines = fileOnlineService.getAllBaseFileOnline(fileOnline);
            page.setResult(baseFileOnlines);
            SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
            //循环list，处理数据
            for (FileOnline b : baseFileOnlines) {
                String createTimeStr = "";
                if (b.getCreateTime() != null) {
                    createTimeStr = sdf.format(b.getCreateTime());
                }
                String createUserName = userService.getUserNameById(b.getCreateUserId());
                b.setCreateTimeStr(createTimeStr);
                b.setCreateUserName(createUserName);
            }
            result.put("count", page.getTotalCount());
            result.put("data", baseFileOnlines);
            result.put("code", "0");
            result.put("msg", "加载完成");
        } catch (Exception e) {
            logger.error("获取在线文件数据失败", e);
            addFailMessage(result, "获取在线文件数据失败");
        }
        return result;
    }

}
